package com.demo.day418;

import java.util.Arrays;

public class HomeWorks {
    /**
     * @Auther: 86176
     * @Date: 2021/4/18 - 04 - 18 - 22:24
     * @Description: com.demo.day418
     * @version: 1.0
     * 
     */
    public static void f1() {
        int[] arr = {1, 2, 3, 4, 5, 6, 7};

        int max = arr[0], min = arr[0], sum = 0;

        for (int i = 0; i < arr.length; i++) {
            if (max < arr[i]) {
                max = arr[i];
            }
        }
        System.out.println(max);

        for (int i = 0; i < arr.length; i++) {
            if (min > arr[i]) {
                min = arr[i];
            }
        }
        System.out.println(min);

        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        System.out.println(sum);

    }

    //  2.对于给定的一个数组,int[] a = {6,3,1,4,7,9,8,10,45,32,15} ，并以升序或者降序排序。（不能用Arrays类）三个题。冒泡  插入  快速
    public static void f2() {
        int[] a = {6, 3, 1, 4, 7, 9, 8, 10, 45, 32, 15};

        // 冒泡
        for (int j = 0; j < 999; j++) {
            for (int i = 0; i < a.length - 1; i++) {

                if (a[i] > a[i + 1]) {
                    int num = 0;
                    num = a[i];
                    a[i] = a[i + 1];
                    a[i + 1] = num;
                }

            }
            if (a[0] == 1) break;
        }
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + "\t");
        }

        System.out.println();
        // 插入
        for (int i = 1; i < a.length; i++) {
            for (int j = i + 1; j < a.length; j++) {
                int num = 0;
                if (a[j] < a[i]) {
                    num = a[i];
                    a[i] = a[i + 1];
                    a[i + 1] = a[i];
                }
            }
        }
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + "\t");
        }

        // 快速
        System.out.println();
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = 0; j < a.length; j++) {
                int num = 0;
                if (a[i] > a[i + 1]) {
                    num = a[i];
                    a[i] = a[i + 1];
                    a[i + 1] = num;
                }
            }
        }
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + "\t");
        }
    }

    //5.编写程序，给定一个整数，按相反顺序输出该数。例如，754321， 控制台输出为7654321。
    public static void f3() {
        int a = 1234567;
        int b;
        for (int i = 0; i < 999; i++) {
            b = a % 10;
            a = a / 10;
            System.out.print(b);
            if (a == 0) break;
        }
    }

    //6.编程输出1~100中能被3整除但不能被5整除的数,并统计有多少个这样的数
    public static void f4() {
        int count = 0;
        for (int i = 1; i <= 100; i++) {
            if (i % 3 == 0 && i % 5 != 0) {
                System.out.print(i + "\t");
                count++;
            }
        }
        System.out.println();
        System.out.print("一共有" + count);
    }

    //7.打印出100-999之间所有的水仙花数, 例如：153是一个"水仙花数"，因为153= 1的三次方＋5的三次方＋3的三次方
    public static void f5() {
        for (int i = 1; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                for (int k = 0; k < 10; k++) {
                    if (i * i * i + j * j * j + k * k * k == i * 100 + j * 10 + k) {
                        System.out.print(i * 100 + j * 10 + k + "\t");
                    }
                }
            }
        }
    }

    //8. 1，2，3，4四个数字，看这四个数字能组成多少个互不相同且无重复数字的三位数？都是多少？
    public static void f6() {
        int count = 0;
        for (int i = 1; i < 5; i++) {
            for (int j = 1; j < 5; j++) {
                for (int k = 1; k < 5; k++) {
                    if (i != j && i != k && j != k) {
                        System.out.print(100 * i + 10 * j + k + "\t");
                        count++;
                    }
                }
            }
        }
        System.out.println("一共有" + count + "个数");
    }

    //9.给一个数组 int[] a = {3,4,5,6,5,5,3,1,7,9,0,4,3,2,1,2,1,2,1,1}，里边放了是10以内的数字，
    // 找到里边各个数值的出现的次数。 （三种方式：一是数组接收，二是用10个变量接收，三是使用循环嵌套的方式）
    public static void f7() {
        int[] a = {3, 4, 5, 6, 5, 5, 3, 1, 7, 9, 0, 4, 3, 2, 1, 2, 1, 2, 1, 1};

        //数组接受
        int[] arr = new int[10];
        for (int i = 0; i < a.length; i++) {
            arr[a[i]]++;
        }
        System.out.println(Arrays.toString(arr));

        //用10个变量接收
        int num = 0;
        int a0 = 0, a1 = 0, a2 = 0, a3 = 0, a4 = 0, a5 = 0, a6 = 0, a7 = 0, a8 = 0, a9 = 0;
        for (int i = 0; i < a.length; i++) {
            num = a[i];
            switch (num) {
                case 0:
                    a0++;
                    break;
                case 1:
                    a1++;
                    break;
                case 2:
                    a2++;
                    break;
                case 3:
                    a3++;
                    break;
                case 4:
                    a4++;
                    break;
                case 5:
                    a5++;
                    break;
                case 6:
                    a6++;
                    break;
                case 7:
                    a7++;
                    break;
                case 8:
                    a8++;
                    break;
                case 9:
                    a9++;
                    break;
            }
        }
        System.out.println(a0 + "\t" + a1 + "\t" + a2 + "\t" + a3 + "\t" + a4 + "\t" + a5 + "\t" + a6 + "\t" + a7 + "\t" + a8 + "\t" + a9 + "\t");

        //循环嵌套
        int[] nums = new int[10];
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < a.length; j++) {
                if (a[j] == i) {
                    nums[i]++;
                }
            }
        }
        System.out.println(Arrays.toString(nums));

    }

    //10.现在定义如下的一个数组：int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5},
    // 要求将以上数组中值为0的去掉，将不为0的值存入一个新的数组，生成的新数组为：int newArr[]={1,3,4,5,6,6,5,4,7,6,7,5}
    public static void f8() {
        int oldArr[] = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5};
        int length = 0;
        for (int i = 0; i < oldArr.length; i++) {
            if (oldArr[i] != 0) {
                length++;
            }
        }
        int[] NewArr = new int[length];
        int num = 0, nums = 0;
        for (int i = 0; i < oldArr.length; i++) {
            if (oldArr[i] != 0) {
                num = oldArr[i];
                NewArr[nums] = num;
                nums++;
            }
        }
        System.out.println(Arrays.toString(NewArr));

    }

    //11.将一个数组进行逆序放在一个新的数组里边，然后输出新的数组
    public static void f9() {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
        int num = 0;
        int[] arr1 = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            num = arr[i];
            arr1[arr.length - 1 - i] = num;
        }
        System.out.println(Arrays.toString(arr1));
    }

    //12.求一个矩阵对角线元素之和
    //	1  2  3  4
    //	4  5  6	 5
    //	7  8  9	 6
    //	1  2  3  4
    public static void f10() {
        int n = 4;
        int[][] arr = {{1, 2, 3, 4}, {4, 5, 6, 5}, {7, 8, 9, 6}, {1, 2, 3, 4}};
        int count1 = 0, count2 = 0;
        for (int i = 0; i < arr.length; i++) {
            count1 += arr[i][i];
            count2 += arr[i][arr.length - 1 - i];
        }
        System.out.println(count1 + "和" + count2);
    }

    //13.得到如图所示的二维数组：
    //	1	6	10	13	15
    //	2	7	11	14
    //	3	8	12
    //	4	9
    //	5
    public static void f11() {
        int num = 1;
        int[][] arr = new int[5][];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = new int[arr.length - i];
        }

        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                arr[j][i] = num;
                num++;
            }
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.println(Arrays.toString(arr[i]));
        }
    }

    public static void f12() {
        int num = 1;
        int[][] arr = new int[8][];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = new int[arr.length - i];
        }

        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                arr[j][i] = num;
                num++;
                if (num == 6) num = 1;
            }
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.println(Arrays.toString(arr[i]));
        }
    }

    public static void f13() {
        int[][] arr = new int[6][];

        //创建数组
        for (int i = 0; i < arr.length; i++) {
            arr[i] = new int[i + 1];
        }

        //赋值 两边=1
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if (i > 1 && j > 0 && j != arr[i].length - 1) {
                    arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1];
                }
                if (j == 0 || j == arr[i].length - 1) {
                    arr[i][j] = 1;
                }
            }
        }

        for (int i = 0; i < arr.length; i++) {
            System.out.println(Arrays.toString(arr[i]));
        }
    }

    public static void f14() {
        int[][] arr = new int[6][];

        //创建数组
        for (int i = 0; i < arr.length; i++) {
            arr[i] = new int[i + 1];
        }

        //赋值 两边=1
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if (j == 0 || j == arr[i].length - 1) {
                    arr[i][j] = 1;
                }
            }
        }

        for (int i = 0; i < arr.length; i++) {
            System.out.println(Arrays.toString(arr[i]));
        }
    }

    public static void f15(int num) {
        int[] arr = new int[num];

        for (int i = 0; i < arr.length; i++) {
            arr[i] = i + 1;
        }

        while (arr.length >= 3) {
            int[] NewArr = new int[arr.length - 1];
            for (int i = 3; i < arr.length; i++) {
                NewArr[i - 3] = arr[i];
            }
            NewArr[NewArr.length - 2] = arr[0];
            NewArr[NewArr.length - 1] = arr[1];
            arr = NewArr;

        }
        System.out.println("最后一个人是" + arr[1]);

    }

    public static void f16(int num) {
        int[] arr = new int[num];

        for (int i = 0; i < arr.length; i++) {
            arr[i] = i + 1;
        }

        int count = 0, counts = num;

        while (counts > 3) {
            for (int i = 0; i < arr.length; i++) {
                if (arr[i] != 0) {
                    count++;
                }
                if (count == 3) {
                    arr[i] = 0;
                    count = 0;
                    counts--;
                }
            }
        }

        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != 0) {
                if (count == 1) {
                    System.out.println(arr[i]);
                }
                count++;
                if (count==4){
                    count = 1;
                }
            }
        }
    }

    public static void main(String[] args) {
        f15(9);
    }
}
